#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use tokio;
    use halo_model::{ExtensionOperator, GroupVersionKind, GVK};
    use serde::{Deserialize, Serialize};
    
    use crate::extension::index::scheme_manager::SchemeManager;
    use crate::config::app_error::AppResult;

    // 测试用的模拟类型
    #[derive(Debug, Clone, Serialize, Deserialize)]
    struct TestExtension {
        pub name: String,
        pub value: i32,
    }

    impl GVK for TestExtension {
        fn group() -> &'static str { "test.example.com" }
        fn version() -> &'static str { "v1" }
        fn kind() -> &'static str { "TestExtension" }
        fn plural() -> &'static str { "testextensions" }
        fn singular() -> &'static str { "testextension" }
    }

    impl ExtensionOperator for TestExtension {
        // 实现必要的方法
    }

    #[tokio::test]
    async fn test_scheme_manager_basic_operations() -> AppResult<()> {
        let manager = SchemeManager::new();
        
        // 测试初始状态
        assert_eq!(manager.size()?, 0);
        
        // 注册一个 scheme
        manager.register::<TestExtension>().await?;
        
        // 验证注册成功
        assert_eq!(manager.size()?, 1);
        
        // 获取注册的 scheme
        let scheme = manager.get_by_type::<TestExtension>().await?;
        assert_eq!(scheme.group_version_kind.group, "test.example.com");
        assert_eq!(scheme.group_version_kind.version, "v1");
        assert_eq!(scheme.group_version_kind.kind, "TestExtension");
        
        Ok(())
    }

    #[tokio::test]
    async fn test_scheme_manager_concurrent_access() -> AppResult<()> {
        let manager = Arc::new(SchemeManager::new());
        let mut handles = vec![];
        
        // 创建多个并发任务
        for i in 0..10 {
            let manager_clone = Arc::clone(&manager);
            let handle = tokio::spawn(async move {
                // 每个任务都尝试注册相同的 scheme
                manager_clone.register::<TestExtension>().await
            });
            handles.push(handle);
        }
        
        // 等待所有任务完成
        for handle in handles {
            handle.await.unwrap()?;
        }
        
        // 验证只注册了一个 scheme（重复注册应该被忽略）
        assert_eq!(manager.size()?, 1);
        
        Ok(())
    }

    #[tokio::test]
    async fn test_scheme_manager_query_operations() -> AppResult<()> {
        let manager = SchemeManager::new();
        
        // 注册 scheme
        manager.register::<TestExtension>().await?;
        
        // 测试 fetch 方法
        let gvk = GroupVersionKind {
            group: "test.example.com".to_string(),
            version: "v1".to_string(),
            kind: "TestExtension".to_string(),
        };
        
        let found_scheme = manager.fetch(&gvk)?;
        assert!(found_scheme.is_some());
        
        // 测试不存在的 scheme
        let non_existent_gvk = GroupVersionKind {
            group: "non.existent".to_string(),
            version: "v1".to_string(),
            kind: "NonExistent".to_string(),
        };
        
        let not_found = manager.fetch(&non_existent_gvk)?;
        assert!(not_found.is_none());
        
        // 测试 get 方法（应该返回错误）
        let result = manager.get(&non_existent_gvk);
        assert!(result.is_err());
        
        Ok(())
    }

    #[tokio::test]
    async fn test_scheme_manager_unregister() -> AppResult<()> {
        let manager = SchemeManager::new();
        
        // 注册 scheme
        manager.register::<TestExtension>().await?;
        assert_eq!(manager.size()?, 1);
        
        // 获取 scheme 用于注销
        let scheme = manager.get_by_type::<TestExtension>().await?;
        
        // 注销 scheme
        manager.unregister::<TestExtension>(&scheme)?;
        assert_eq!(manager.size()?, 0);
        
        Ok(())
    }

    #[tokio::test]
    async fn test_scheme_manager_register_with_function() -> AppResult<()> {
        let manager = SchemeManager::new();
        
        // 使用 register_fun 方法
        manager.register_fun::<_, TestExtension>(|_index_specs| {
            // 模拟一些配置逻辑
            println!("Configuring index specs for TestExtension");
            Ok(())
        }).await?;
        
        // 验证注册成功
        assert_eq!(manager.size()?, 1);
        
        let scheme = manager.get_by_type::<TestExtension>().await?;
        assert_eq!(scheme.plural, "testextensions");
        assert_eq!(scheme.singular, "testextension");
        
        Ok(())
    }

    #[tokio::test]
    async fn test_scheme_manager_duplicate_registration() -> AppResult<()> {
        let manager = SchemeManager::new();
        
        // 多次注册相同的 scheme
        manager.register::<TestExtension>().await?;
        manager.register::<TestExtension>().await?;
        manager.register::<TestExtension>().await?;
        
        // 应该只有一个 scheme
        assert_eq!(manager.size()?, 1);
        
        Ok(())
    }

    #[tokio::test]
    async fn test_scheme_manager_schemes_method() -> AppResult<()> {
        let manager = SchemeManager::new();
        
        // 注册 scheme
        manager.register::<TestExtension>().await?;
        
        // 获取所有 schemes
        let schemes = manager.schemes()?;
        assert_eq!(schemes.len(), 1);
        assert_eq!(schemes[0].group_version_kind.kind, "TestExtension");
        
        Ok(())
    }

    /// 性能测试：大量并发读写操作
    #[tokio::test]
    async fn test_scheme_manager_performance() -> AppResult<()> {
        let manager = Arc::new(SchemeManager::new());
        
        // 先注册一个 scheme
        manager.register::<TestExtension>().await?;
        
        let mut handles = vec![];
        
        // 创建大量并发读操作
        for _ in 0..100 {
            let manager_clone = Arc::clone(&manager);
            let handle = tokio::spawn(async move {
                // 并发读取操作
                let _scheme = manager_clone.get_by_type::<TestExtension>().await?;
                let _size = manager_clone.size()?;
                let _schemes = manager_clone.schemes()?;
                Ok::<(), crate::config::app_error::AppError>(())
            });
            handles.push(handle);
        }
        
        // 等待所有读操作完成
        for handle in handles {
            handle.await.unwrap()?;
        }
        
        println!("Performance test completed successfully");
        Ok(())
    }
}
